local sk__luoyan = fk.CreateSkill {

  name = "sk__luoyan",

  tags = {  },

}



sk__luoyan:addEffect(fk.EventPhaseStart, {
  name = "sk__luoyan",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__luoyan.name) and player.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#sk__luoyan-choose", sk__luoyan.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAllPlayers()) do
      room:setPlayerMark(p, "@@sk__luoyan", 0)
    end
    room:setPlayerMark(room:getPlayerById(event:getCostData(self)), "@@sk__luoyan", player.id)
  end,
})

sk__luoyan:addLoseEffect(function (self, player, is_death)
     local room = player.room
    for _, p in ipairs(room:getAllPlayers()) do
      room:setPlayerMark(p, "@@sk__luoyan", 0)
    end
end)


sk__luoyan:addEffect(fk.Deathed, {
  refresh_events = {fk.EventLoseSkill, fk.Deathed},
  can_refresh = function (self, event, target, player, data)
    if target == player then
      return player:hasSkill(self, true, true)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAllPlayers()) do
      room:setPlayerMark(p, "@@sk__luoyan", 0)
    end
  end,
})

sk__luoyan:addEffect(fk.CardUseFinished, {
  name = "#sk__luoyan_delay",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:getMark("@@sk__luoyan") ~= 0 and player.phase == Player.Play and not player.dead then
      local n = #player.room.logic:getEventsOfScope(GameEvent.UseCard, 4, function(e)
        local use = e.data
        return use and use.from == player
      end, Player.HistoryPhase)
      if n < 4 then
        event:setCostData(self, n)
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local src = room:getPlayerById(player:getMark("@@sk__luoyan"))
    src:broadcastSkillInvoke("sk__luoyan")
    room:notifySkillInvoked(src, "sk__luoyan", "control")
    room:doIndicate(src.id, {player.id})
    if event:getCostData(self) == 1 then
      if not player:isNude() then
        local card = table.random(player:getCardIds("he"))
        room:throwCard(card, "sk__luoyan", player, player)
      end
    elseif event:getCostData(self) == 2 then
      room:loseHp(player, 1, "sk__luoyan")
    elseif event:getCostData(self) == 3 then
      room:changeMaxHp(player, -1)
    end
  end,
})

return sk__luoyan